package com.example.model_user.register

import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.widget.addTextChangedListener
import com.alibaba.android.arouter.facade.annotation.Route
import com.chendechao.lib_base.BaseActivity
import com.chendechao.lib_common.ARouterCdd
import com.example.model_user.R
import com.example.model_user.databinding.ActivityRegisterBinding
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = ARouterCdd.REGISTER)
class RegisterActivity: BaseActivity<ActivityRegisterBinding,RegisterViewModel>(ActivityRegisterBinding::inflate) {
    override val viewModel: RegisterViewModel by viewModels()
    val mmkv = MMKV.defaultMMKV()
    override fun initView() {
        binding.apply {
            registerUsername.addTextChangedListener { username ->
                viewModel.sendEvent(RegisterIntent.UsernameChanged(username.toString()))

            }
            registerPassword.addTextChangedListener { password ->
                viewModel.sendEvent(RegisterIntent.PasswordChanged(password.toString()))

            }
            register.setOnClickListener {
                mmkv.encode("username",registerUsername.toString())
                mmkv.encode("password",registerPassword.toString())
                viewModel.sendEvent(RegisterIntent.RegisterClicked)
            }
            registerBack.setOnClickListener {

                finish()
            }
        }

    }

    override fun initData() {
        collectState(viewModel.state){state ->
            updateUI(state)
        }

    }
    private fun updateUI(state: RegisterUiState){
        binding.apply {
            if(state.isLoading){

            }
            if(state.isSucess){
                finish()
                Log.i("TAG", "updateUI: ${state.userInfo}")
            }
        }
    }


}